home *** CD-ROM | disk | FTP | other *** search
- Path: news.uni-stuttgart.de!schweikh
- From: schweikh@itosun.ito.uni-stuttgart.de (Jens Schweikhardt)
- Newsgroups: comp.std.c
- Subject: Re: int's and zero
- Date: 9 Jan 1996 11:44:42 GMT
- Organization: Comp.Center (RUS), U of Stuttgart, FRG
- Message-ID: <4ctkfa$1fj0@info4.rus.uni-stuttgart.de>
- References: <4cth4e$4q@odin.funcom.no>
- NNTP-Posting-Host: itosun.ito.uni-stuttgart.de
-
- In article <4cth4e$4q@odin.funcom.no>,
- Eivind Eklund <eivind@odin.funcom.com> wrote:
- >Are zero in an integer required to be represented by binary zeros?
- >I know it is not required for floats and pointers, so memset()ing a
- >structure to 8-bit zeros are not correct if it contain pointers or
- >floating point values, but what about integers? Ie, if I have
- >
- >struct intstruct {
- > int a;
- > long b;
- > short c;
- >};
- >..
- >struct intstruct teststruct;
- >memset(&teststruct, 0, sizeof(teststruct));
- >
- >Is teststruct.a, teststruct.b, and teststruct.c guaranteed to be == 0?
-
- Eivind,
-
- there is an (elegant, IMHO) way to initialize structures
- as if you had written <member> = 0 for any member.
- The standard requires this for objects of static storage type.
- It also has the advantage to work for pointers and floating point
- as well as numbers. It also applies recursively to structs and
- unions within a struct or union. Just make one
- structure static and assign it to whatever other (maybe
- automatic) struct you have. E.g.
-
- static struct intstruct {
- int a;
- long b;
- short c;
- double d;
- char *e;
- } zero;
-
- ...
-
- void
- foo (void)
- {
- struct intstruct { ... } bar;
- ...
- bar = zero; /* Much more readable than memset(&bar, 0, sizeof bar); */
- }
-
- Good compilers will certainly use a memcpy equivalent
- to code the strcuture assignment.
-
- Bye, Jens
- --
- SIGSIG -- signature too long (core dumped)
-